﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using Sigecov.Formularios;
using System.Data;
using System.Runtime.InteropServices;
namespace Sigecov.Models
{
    class Ferramenta
    {
        public static void DesativarControles(System.Windows.Forms.Control.ControlCollection controles)
        {
            foreach (Control controle in controles)
            {
                if (controle.GetType() == typeof(TextBox))
                    (controle as TextBox).Enabled = false;
                else if (controle.GetType() == typeof(ComboBox))
                    (controle as ComboBox).Enabled = false;
                else if (controle.GetType() == typeof(MaskedTextBox))
                    (controle as MaskedTextBox).Enabled = false;
                else if (controle.GetType() == typeof(RadioButton))
                    (controle as RadioButton).Enabled = false;
                else if (controle.GetType() == typeof(Label))
                    (controle as Label).Enabled = false;
                else if (controle.GetType() == typeof(GroupBox))
                    (controle as GroupBox).Enabled = false;
                else if (controle.GetType() == typeof(DateTimePicker))
                    (controle as DateTimePicker).Enabled = false;
                else if (controle.GetType() == typeof(RichTextBox))
                    (controle as RichTextBox).Enabled = false;
            }
        }
        public static void ResetarControles(System.Windows.Forms.Control.ControlCollection controles)
        {
            foreach (Control controle in controles)
            {
                if (controle.GetType() == typeof(TextBox))
                    (controle as TextBox).Clear();
                else if (controle.GetType() == typeof(MaskedTextBox))
                    (controle as MaskedTextBox).Clear();
                else if (controle.GetType() == typeof(ComboBox))
                {
                    if ((controle as ComboBox).Items.Count > 0)
                        (controle as ComboBox).SelectedIndex = 0;
                }
                else if (controle.GetType() == typeof(RichTextBox))
                    (controle as RichTextBox).Clear();
                else if (controle.GetType() == typeof(DateTimePicker))
                    (controle as DateTimePicker).Text = DateTime.Now.ToLocalTime().ToString();
            }
        }
        public static Form GerarFormularioPesquisa(List<object> Dados)
        {
            FrmPesquisa formularioPesquisa = new FrmPesquisa();
            formularioPesquisa.dgvDadosPesquisa.DataSource = Dados;
            return formularioPesquisa;
        }
        public static void SomenteNumero(KeyPressEventArgs e)
        {
            if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || (e.KeyChar.CompareTo((char)8)).ToString() == "0"))
                e.Handled = true;
            else
                e.Handled = false;

        }
        public static void ValorMonetario(KeyPressEventArgs e)
        {
            if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar == '.' || (e.KeyChar.CompareTo((char)8)).ToString() == "0"))
                e.Handled = true;
            else
                e.Handled = false;
        }
        public static Dictionary<string,string> BuscarCep(string cep)
        {
            Dictionary<string, string> endereco = new Dictionary<string, string>();
            try
            {
                
                DataSet ds = new DataSet();
                ds.ReadXml("http://cep.republicavirtual.com.br/web_cep.php?cep=" + cep.Replace("-", "").Trim() + "&formato=xml");
                if (ds != null)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        endereco.Add("tipoLogradouro", ds.Tables[0].Rows[0]["tipo_logradouro"].ToString().Trim());
                        endereco.Add("logradouro", ds.Tables[0].Rows[0]["logradouro"].ToString().Trim());
                        endereco.Add("bairro", ds.Tables[0].Rows[0]["bairro"].ToString().Trim());
                        endereco.Add("estado", ds.Tables[0].Rows[0]["uf"].ToString().Trim());
                        endereco.Add("cidade", ds.Tables[0].Rows[0]["cidade"].ToString().Trim());
                        return endereco;
                    }
                    else
                        return endereco = null;
                }
                else
                    return endereco = null;
            }
            catch
            {
                endereco.Add("conexao", "Espere a conexão com a internet estabilizar.");
                return endereco = null;
            }
        }
        [DllImport("wininet.dll")]
        private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);
        public static bool VerificarConexaoInternet()
        {
            int desc;
            return InternetGetConnectedState(out desc, 0);
        }
    }            
}
